# encoding: utf-8

class Article
  include Mongoid::Document
  field :title, type: String
  field :content, type: String
  field :views, type: Integer, default: 0
  field :enable, type: Integer, default: true
  field :comment_count, type: Integer, default: 0
  field :created_at, type: DateTime
  field :updated_at, type: DateTime

  belongs_to :user
  embeds_many :comments

  def safe_attributes
    self.as_json(only: [:_id, :title, :content, :views, :user, :created_at, :updated_at, :comment_count],
                 include: {user: {only: [:_id, :name, :username]}})
  end

  def paginate_comments(start=0, _end =100)
    return self.comments[start.to_i.._end.to_i], self.comments.count
  end

  class << self
    def paginate(start=0, _end=100)
      return Article.includes(:user).order(created_at: :desc)[start.to_i.._end.to_i], Article.count
    end

  end
end